/*
 *
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding UTF-8
 */
package net.comm.group.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;


import net.comm.friend.bean.BaseFriend;
import net.comm.friend.service.impl.FriendServiceImpl;
import net.comm.friend.service.impl.LinjuServiceImpl;
import net.comm.friend.web.form.FriendForm;
import net.comm.friend.web.form.LinjuForm;
import net.comm.group.bean.BaseGPicture;
import net.comm.group.service.impl.GAlbumServiceImpl;
import net.comm.group.service.impl.GPictureServiceImpl;
import net.comm.group.service.impl.GdianpingServiceImpl;
import net.comm.group.web.form.GAlbumForm;
import net.comm.group.web.form.GPictureForm;
import net.comm.group.web.form.GdianpingForm;
import net.comm.page.PageCtr;
import net.comm.util.HtmlMessageBox;
import net.comm.web.base.AbstractBaseAction;


/**
 *群相片Action
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class GPictureAction extends AbstractBaseAction {
	
	private GPictureServiceImpl service = null;
	
	/**
	 * struct-config中该ACTION中的每个forward的常量定义
	 */
	final String LIST_FORWARD = "list";
	final String OPER_FORWARD = "oper";
	final String INFO_FORWARD = "info";
	final String LOGIN_FORWARD = "loginFwd";
	
	
	/**
	 *构造方法
	 */
	public GPictureAction()
	{
		super(false);
		
		/**
		 * 初始化业务组件接口
		 */
		service = new GPictureServiceImpl();
	}
	
	/**
	 * 默认动作
	 * 1.action值为空的时候,进入查询页面
	 * 2.每个操作都执行的代码,但是方法必须返回null
	 * @param mapping	 跳转的mapping值	
	 * @param request	 HttpServletRequest对象
	 * @param response	 HttpServletResponse对象
	 */
	public ActionForward startAction(ActionMapping mapping, ActionForm form,
									 HttpServletRequest request , 
									 HttpServletResponse response)
	{
		String action = getReqParam(request , ACTION_KEY);
		if(action == null || action.trim().length() == 0) {
			return mapping.findForward(LIST_FORWARD);
		} else {
			return null;
		}
	}
	
	/**
	 * 查询群相片清单
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		GPictureForm theForm = (GPictureForm)form;
			
		// 生成分页对象	
		theForm.setPage(this.getPageFromReq(request,40));
		
		ReturnMessage msg  = new ReturnMessage();
		
		/**
		 *赋值查询条件
		 */
		
		/**
		 * 设置当前页面key和上页lastKey
		 */
		theForm.setLastKey(getReqParam(request, KEY));
		/**
		 *调用services中的方法
		 */
		List list = service.list(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		/**
		 *设置列表的数据
		 */
		setAction(request, "list");
		request.setAttribute("piclist" , list);
		request.setAttribute("theForm", theForm);
		request.setAttribute("picnum", list.size());

		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}
	
	/**
	 * 查询群相片清单
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward adlist(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		GPictureForm theForm = (GPictureForm)form;
			
		// 生成分页对象	
		theForm.setPage(this.getPageFromReq(request,40));
		
		ReturnMessage msg  = new ReturnMessage();
		
		//取得当前浏览者信息
		Integer userID = (Integer)request.getSession().getAttribute(GlbFinal.SESSION_USERID);
		if(userID == null || userID == 0 ){
			return mapping.findForward(LOGIN_FORWARD);
		}
		
		// 取得相册信息
		GAlbumServiceImpl albumservice = new GAlbumServiceImpl();
		GAlbumForm albumForm  = new GAlbumForm();
		
		albumForm.setGalbumID(theForm.getQryGAlbumID());
		
		albumservice.get(albumForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryGroupID(albumForm.getGroupID());
		request.setAttribute("albumForm" , albumForm);
		
		if(albumForm.getProtect().equals(GlbFinal.USERSET_BAOMI)&&
				albumForm.getUserID() != userID){
			msg.setMsg("相册不公开");
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);			
		}else
		//判断相册隐私设置，如果是对邻居开放，则判断是否是邻居关系
		if(albumForm.getProtect().equals(GlbFinal.USERSET_LINJU)&&
				albumForm.getUserID() != userID ){
			
			LinjuServiceImpl linjuservice = new LinjuServiceImpl();
			LinjuForm linjuform = new LinjuForm();
			
			linjuform.setUserID(albumForm.getUserID());
			linjuform.setLinjuID(userID);
			linjuservice.get(linjuform, msg);
			if(msg.getErrCode() != 0){
				msg.setMsg("相册只对邻居开放");
				HtmlMessageBox hmb = new HtmlMessageBox();
				hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
				hmb.setPrintMessage(msg.getMsg());
				hmb.setForwardUrl("javascript:history.go(-1)");
				return showMsg(hmb , request , response , mapping);			
			}
		}else
		//如果对好友开放，则判断是否是好友关系
		if(albumForm.getProtect().equals(GlbFinal.USERSET_FRIEND)&&
				albumForm.getUserID() != userID){
				
			FriendServiceImpl friendservice = new FriendServiceImpl();
			if(!friendservice.isFriend(albumForm.getUserID(), userID)){
				msg.setMsg("相册只对好友开放");
				HtmlMessageBox hmb = new HtmlMessageBox();
				hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
				hmb.setPrintMessage(msg.getMsg());
				hmb.setForwardUrl("javascript:history.go(-1)");
				return showMsg(hmb , request , response , mapping);			
			}
		}
		/**
		 *调用services中的方法
		 */
		List list = service.adlist(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		if(list.size() > 0){
			BaseGPicture base = (BaseGPicture)list.get(0);
			theForm.setUserID(base.getUserID());
		}
		/**
		 *设置列表的数据
		 */
		setAction(request, "list");
		request.setAttribute("piclist" , list);
		request.setAttribute("theForm", theForm);
		request.setAttribute("picnum", list.size());
		
		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}
	/**
	 * 查询群相片清单
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward viewlist(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		GPictureForm theForm = (GPictureForm)form;
			
		// 生成分页对象	
		theForm.setPage(this.getPageFromReq(request,40));
		
		ReturnMessage msg  = new ReturnMessage();
		
		//取得当前浏览会员信息
		Integer userID = (Integer)request.getSession().getAttribute(GlbFinal.SESSION_USERID);
		if(userID == null){
			
			userID = 0;
		}
		
		//判断当前浏览会员是否是好友/ 邻居
		FriendServiceImpl friendservice = new FriendServiceImpl();
		FriendForm friendForm = new FriendForm();
		
		friendForm.setQryUserID(theForm.getUserID());
		friendForm.setQryFriendID(userID);
		
		List firendlist = friendservice.adlist(friendForm, msg);
		if(msg.getErrCode() == GlbFinal.ERRCODE_SUCCESS && firendlist.size() > 0) 
		{
			BaseFriend base = (BaseFriend)firendlist.get(0);
			if(base.getTeamID() == GlbFinal.LINJUTEAM){
				
				request.setAttribute("isLinju" , GlbFinal.YES);	
			}else{
				request.setAttribute("isFriend" , GlbFinal.YES);	
			}
		}
		
		// 取得相册信息
		GAlbumServiceImpl albumservice = new GAlbumServiceImpl();
		GAlbumForm albumForm  = new GAlbumForm();
		
		albumForm.setGalbumID(theForm.getQryGAlbumID());
		
		albumservice.get(albumForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryGroupID(albumForm.getGroupID());
		request.setAttribute("albumForm" , albumForm);		
		
		/**
		 *调用services中的方法
		 */
		List list = service.adlist(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		/**
		 *设置列表的数据
		 */
		setAction(request, "list");
		request.setAttribute("piclist" , list);
		request.setAttribute("theForm", theForm);
		request.setAttribute("picnum", list.size());
		
		
		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}	
	/**
	 *浏览相片
	 */
	public ActionForward views(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GPictureForm theForm = (GPictureForm) form;
		
		theForm.setPage(this.getPageFromReq(request, 20));
		ReturnMessage msg  = new ReturnMessage();
		
		List list = service.views(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		request.setAttribute("list", list);
		request.setAttribute("theForm", theForm);

		
		// 取得相册信息
		GAlbumServiceImpl albumservice = new GAlbumServiceImpl();
		GAlbumForm albumForm  = new GAlbumForm();
		
		albumForm.setGalbumID(theForm.getQryGAlbumID());
		
		albumservice.get(albumForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		request.setAttribute("albumForm" , albumForm);		
		
		theForm.setQryGroupID(albumForm.getGroupID());
		
		//取得相片点评信息
		GdianpingServiceImpl dianpingservice = new GdianpingServiceImpl();
		GdianpingForm dianpingform = new GdianpingForm();
		
		dianpingform.setPage(theForm.getPage());
		dianpingform.setQryDsort(GlbFinal.DSORT_PIC);
		dianpingform.setQryDsortID(theForm.getQryGpicID());
		
		List dianpinglist = dianpingservice.list(dianpingform, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		request.setAttribute("dianpinglist" , dianpinglist);	
		request.setAttribute("dianpingform" , dianpingform);		
		
		theForm.setPage(dianpingform.getPage());
		
		setAction(request, "views");
		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , dianpingform.getPage());
		request.setAttribute(KEY , dianpingform.getKey());
		request.setAttribute(LAST_KEY , dianpingform.getLastKey());		
		
		return mapping.findForward(INFO_FORWARD);
	}	
	
	/**
	 *取群相片
	 */
	public ActionForward get(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GPictureForm theForm = (GPictureForm) form;
		
		ReturnMessage msg  = new ReturnMessage();
		
		service.get(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		request.setAttribute("theForm", theForm);
		
		// 取得相册信息
		GAlbumForm albumForm  = new GAlbumForm();
		
		albumForm.setGalbumID(theForm.getGalbumID());
		
		GAlbumServiceImpl albumservice = new GAlbumServiceImpl();
		
		albumservice.get(albumForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		request.setAttribute("albumForm" , albumForm);		
		
		
		return mapping.findForward(INFO_FORWARD);

	}	
	/**
	 *增加群相片
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GPictureForm theForm = (GPictureForm) form;
		
		String path = (String)request.getSession().getServletContext().getRealPath("/");

		System.out.println("s="+ path);
		
		ReturnMessage msg  = new ReturnMessage();
		
		service.add(theForm, path, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryGAlbumID(theForm.getGalbumID());
		
		request.setAttribute("theForm", theForm);
		
		return adlist(mapping, form, request, response);
	}
	/**
	 *删除群相片
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GPictureForm theForm = (GPictureForm) form;
		
		ReturnMessage msg  = new ReturnMessage();
		
		service.delete(theForm,  msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryGAlbumID(theForm.getGalbumID());
		
		request.setAttribute("theForm", theForm);
		
		return adlist(mapping, form, request, response);
	}	
	/**
	 *增加会员相片
	 */
	public ActionForward uadd(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		GPictureForm theForm = (GPictureForm) form;
		
		String path = (String)request.getSession().getServletContext().getRealPath("/");

		ReturnMessage msg  = new ReturnMessage();
		
		service.uadd(theForm, path, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}else{
			msg.setMsg(" 照片上传成功！");
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_PRINTTOHTML);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl(GlbFinal.FOLDER_INSTALL + "/users.do?action=main");
			return showMsg(hmb , request , response , mapping);
		}
	}	
	
	/**
	 * 跳转到显示页面
	 */
	public ActionForward addPage(ActionMapping mapping, ActionForm form,
			 				 	 HttpServletRequest request , 
			 				 	 HttpServletResponse response)  
		throws Exception
	{
		GPictureForm theForm = (GPictureForm) form;
	
		Integer userID = (Integer)request.getSession().getAttribute(GlbFinal.SESSION_USERID);
		if(userID == null || userID == 0){
			
			return mapping.findForward(LOGIN_FORWARD);
		}
		theForm.setUserID(userID);

		
		GAlbumServiceImpl albumservice = new GAlbumServiceImpl();
		GAlbumForm albumForm = new GAlbumForm();
		ReturnMessage msg  = new ReturnMessage();

		PageCtr page = new PageCtr(1, 100, "N", " 1=1 ");
		albumForm.setPage(page);
		if(theForm.getQryGroupID() != GlbFinal.PUBLICID){
			//取得群组所有的相册
			albumForm.setQryGroupID(theForm.getQryGroupID());
		}else{
			//取得会员所有的相册
			albumForm.setQryUserID(theForm.getUserID());
		}
		List albumlist = albumservice.adlist(albumForm, msg);
		
		//如果为空 则建立默认专辑
		if(albumlist.size() == 0){
			
			albumForm.setAlbumname("默认相册");
			albumForm.setGroupID(theForm.getQryGroupID());
			albumForm.setAlbuminfo("");
			albumForm.setUserID(userID);
			albumForm.setProtect(GlbFinal.USERSET_MEMBER);
			albumForm.setDelflg(GlbFinal.YES);
			String path = (String)request.getSession().getServletContext().getRealPath("/");
			
			albumservice.add(albumForm,path, msg);
			if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
			{
				HtmlMessageBox hmb = new HtmlMessageBox();
				hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
				hmb.setPrintMessage(msg.getMsg());
				hmb.setForwardUrl("javascript:history.go(-1)");
				return showMsg(hmb , request , response , mapping);
			}			
			albumlist = albumservice.list(albumForm, msg);
		}
		
		request.setAttribute("albumlist", albumlist);
		request.setAttribute("theForm", theForm);
		
		return mapping.findForward(OPER_FORWARD);
	}	

}
